/**
 * 1. 进入页面首先要获取部门信息和职务信息，绑定到执行的下拉框中
 * 2. 获取用户输入的信息
 *    格式校验
 * 发送请求
 */
 let jobAddModule = (function () {
    // 获取需要操作的元素
    let $jobname = $('.jobname'),
        $spanjobname = $('.spanjobname'),
        $jobdesc= $('.jobdesc'),
        $spanjobdesc = $('.spanjobdesc'),
        $spanuserright = $('.spanuserright')
        $submit = $('.submit'),
        $checkboxList = $('input[type="checkbox"]'),
        $length = 0,
         jobId = null
    
    //表单校验
    function checkName(){
        let val = $jobname.val().trim()
        if(val.length == 0){
            $spanjobname.html('小主。当前是必填项哦~~')
            return  false
        }
        if(val.length > 15){
            $spanjobname.html('小主。部门名称不能超过15个字~~')
            return false
        }
    }
    function checkDesc(){
        let val = $jobdesc.val().trim()
        if(val.length == 0){
            $spanjobdesc.html('小主。当前是必填项哦~~')
            return  false
        }
        if(val.length > 25){
            $spanjobdesc.html('小主。部门名称不能超过15个字~~')
            return false
        }
       
    }
    function checkPower(){
          console.log($checkboxList)s
          if($length ==  0){
            $spanuserright.html('小主，至少为用户分配一种权限哦~~·')
              return  false
          }
    }
    // 点击确认提交按钮
    async function submitHandle(){
        // 点击按钮校验权限
        checkPower()
        // 表单校验
        if(!checkName() || !checkEmail() || !checkPower())  return
        // 获取用户输入的信息
        let params = {
            name:$username.val().trim(),
            sex:$userdepartment.prop('checked')?0:1,
            email:$useremail.val().trim(),
            phone:$userphone.val().trim(),
            departmentId:$userdepartment.val(),
            jobId:$userjob.val(),
            desc:$userdesc.val().trim()
        }
        // 区分是新增一条数据还是修改
        if(userId) {
            // 向服务器发送请求
            params.userId = userId
        let result = await axios.post('/user/update',params)
        if(result.code == 0){
            alert('小主您很棒，已为您成功新增一条数据')
            window.location.href = 'userlist.html'
            return
        }
        alert('当前网络繁忙，请刷新后重试')
        return
        }
        // 向服务器发送请求
        let result = await axios.post('/job/add',params)
        if(result.code == 0){
            alert('小主您很棒，已为您成功新增一条数据')
            window.location.href = 'userlist.html'
            return
        }
        alert('当前网络繁忙，请刷新后重试')
    }
    // 从服务器获取用户的基本信息
    async function queryBaseInfo(){
       let result = await axios.get('/job/info',{
           params:{
               jobId
           }
       })
    console.log(result,'jobInfo')
       if(result.code == 0){
           result = result.data
          $jobname.val(result.name);
          $jobdesc.val(result.desc)
           return
       }
       alert('小主，当前要编辑的员工不存在，请查证！')
       jobId = null
    }
    return {
        init() {

           // 获取userId,获取员工传递的基本信息
           let params = window.location.href.queryURLParams()
           if(params.hasOwnProperty('id')){
               jobId = params.id
               console.log(jobId)
               queryBaseInfo(jobId)
           }
           // 文本框失去焦点校验
           $jobname.blur(checkName);
           $jobdesc.blur(checkDesc)
           
           $submit.click(submitHandle)
        //    submitHandle()
        }
    }
})()

jobAddModule.init()